package ca.uwindsor;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class AddPreviousCGPA extends Activity {


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_previous_cgpa);

		Button add_but = (Button) findViewById(R.id.add_button);

		// Alert Dialog builder
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);

		//load values from database
		final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
		db.setDATABASE_TABLE("users");
		db.open();
		Cursor ret = db.getAllEntries();
		if(ret.moveToFirst()){
			EditText credit = (EditText) findViewById(R.id.creditcomp);
			EditText cgpa = (EditText) findViewById(R.id.cgpa);
			credit.setText(ret.getString(4));
			cgpa.setText(ret.getString(3));
		}
		
		db.close();
		
		add_but.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
				db.setDATABASE_TABLE("users");
				db.open();
				EditText credit = (EditText) findViewById(R.id.creditcomp);
				EditText cgpa = (EditText) findViewById(R.id.cgpa);
				if(cgpa.getEditableText().toString().trim().equals("") ||
						credit.getEditableText().toString().trim().equals(""))
						
				{
					alert.setMessage("All fields are required");
					alert.setNegativeButton("OK", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
						}

					});
					alert.show();
				}//check cgpa range
				else if((Integer.parseInt(cgpa.getEditableText().toString())>13) ||
						(Integer.parseInt(cgpa.getEditableText().toString())<0))
				{
					alert.setMessage("CGPA should be between 0 and 13");
					alert.setNegativeButton("OK", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
						}

					});
					alert.show();
					
				}else{//add values
				ContentValues newValues = new ContentValues();

				newValues.put("prev_GPA", cgpa.getText().toString());

				newValues.put("credit_com", credit.getText().toString());

				db.updateEntry("_id", "1", newValues);

				alert.setMessage("Previous CGPA added ");

				alert.setNegativeButton("OK",
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								startActivity(new Intent(
										"ca.uwindsor.SUMMARY"));
							}

						});
				alert.setCancelable(false);
				alert.show();

				db.close();
			}
			}
		});

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}
}
